using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fuzhu;
using Dapper;
using LKU8.shoukuan.Model;

namespace LKU8.shoukuan
{
    public class Rd10Service
    {

        private static WriteLog wl = new WriteLog();
        /// <summary>
        /// 检查生产入库单是否已存在
        /// </summary>
        /// <param name="cMoCode">工单号</param>
        /// <param name="constr">连接字符串</param>
        /// <returns></returns>
        public static bool IsRd10Exists(WanJi_Rd10_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT COUNT(*) FROM rdrecord10 a
inner join rdrecords10 b on a.id= b.id
inner join rdrecords10_extradefine ex on b.autoid= ex.autoid 
WHERE a.dDate=@dDate and b.cmocode=@cMoCode and ex.cBDefine2=@cBanCi";
                int count = conn.Query<int>(sql, entity).FirstOrDefault();
                return count > 0;
            }
        }


        /// <summary>
        /// 读取入库单明细id
        /// </summary>
        /// <param name="cMoCode">工单号</param>
        /// <param name="constr">连接字符串</param>
        /// <returns></returns>
        public static int GetRd10Atuoid(WanJi_Rd10_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT b.autoid FROM rdrecord10 a
inner join rdrecords10 b on a.id= b.id
inner join rdrecords10_extradefine ex on b.autoid= ex.autoid 
WHERE a.dDate=@dDate and b.cmocode=@cMoCode and ex.cBDefine2=@cBanCi";
                int autoid = conn.Query<int>(sql, entity).FirstOrDefault();
                return autoid;
            }
        }
        /// <summary>
        /// 检查生产工时是否已存在
        /// </summary>
        /// <param name="cMoCode">工单号</param>
        /// <param name="constr">连接字符串</param>
        /// <returns></returns>
        public static bool isWorkHourExists(WanJi_Rd10_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"SELECT count(*) FROM zdy_wanji_gongshi a
 
WHERE autoid =@autoid and dworkhr>0";
                int count = conn.Query<int>(sql, entity).FirstOrDefault();
                return count > 0;
            }
        }
        /// <summary>
        ///  根据日期+生产订单+线别+存货编码+班次
        /// </summary>
        /// <param name="entity">生产计划实体</param>
        /// <param name="constr">连接字符串</param>
        /// <returns>是否存在</returns> 
        public static string GetPlanCode(WanJi_Rd10_View entity, string constr)
        {
             
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"select  ccode
from zdy_wanji_jihua 
where convert(varchar(20),ddate,23) =convert(varchar(20), @dDate,23) and cMocode = @cMoCode and cXianbie = @cXianBie and cInvcode = @cInvCode
and cBanci= @cBanCi";
                var parameters = new
                {
                    dDate = entity.dDate,
                    cMoCode = entity.cMoCode,
                    cXianBie = entity.cXianBie,
                    cInvCode = entity.cInvCode,
                    cBanCi = entity.cBanCi
                };
                //wl.WriteLogs("读取计划编码"+parameters.ToString());
                var vcode = conn.Query<string>(sql, parameters);
               // wl.WriteLogs("读取计划编码" + DbHelper.GetDbString(vcode.FirstOrDefault()));
                return vcode.FirstOrDefault() ?? "";
            }
        }

        /// <summary>
        /// 读取线别信息
        /// </summary>
        /// <param name="entity">生产计划实体</param>
        /// <param name="constr">连接字符串</param>
        /// <returns>是否存在</returns> 
        public static WanJi_XianBie_Entity GetXianBieEntity(string _XianBie, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = @"select cxianbie,cgongxu, cchangqu from zdy_wanji_xianbie
 
where cxianbie = @cxianbie  ";

                WanJi_XianBie_Entity entity = conn.Query<WanJi_XianBie_Entity>(sql, new { cxianbie = _XianBie }).FirstOrDefault();
                return entity;
            }
        }
         
    }
}